package com.xbts.system.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xbts.common.exception.ServiceException;
import com.xbts.system.domain.CnCloudMarket;
import com.xbts.system.domain.CnGovernmentCloud;
import com.xbts.system.mapper.CnCloudMarketTypeMapper;
import com.xbts.system.service.ICnCloudMarketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xbts.system.domain.CnCloudMarketType;
import com.xbts.system.service.ICnCloudMarketTypeService;

/**
 * 云市场类型Service业务层处理
 *
 * @author xbts
 * @date 2023-10-10
 */
@Service
public class CnCloudMarketTypeServiceImpl  extends ServiceImpl<CnCloudMarketTypeMapper, CnCloudMarketType> implements ICnCloudMarketTypeService {


    @Autowired
    private ICnCloudMarketService cnCloudMarketService;

    /**
     * 查询云市场类型列表
     *
     * @return 云市场类型
     */
    @Override
    public List<Map<String, Object>> selectCnCloudMarketTypeList() {
        List<CnCloudMarketType> list = list();
        if (list == null || list.size() == 0) {
            return new ArrayList<>();
        }

        Map<String, List<CnCloudMarketType>> listMap = list.stream().collect(Collectors.groupingBy(CnCloudMarketType::getType));

        List<Map<String, Object>> result = new ArrayList<>();
        for (Map.Entry<String, List<CnCloudMarketType>> stringListEntry : listMap.entrySet()) {
            Map<String, Object> map = new HashMap<>();
            map.put("key", stringListEntry.getKey());
            map.put("value", stringListEntry.getValue());
            result.add(map);
        }
        return result;
    }

    /**
     * 查询云市场类型
     *
     * @param id 云市场类型主键
     * @return 云市场类型
     */
    @Override
    public CnCloudMarketType selectCnCloudMarketTypeById(String id) {
        return getById(id);
    }

    /**
     * 新增云市场类型
     *
     * @param cnCloudMarketType 云市场类型
     * @return 结果
     */
    @Override
    public boolean insertCnCloudMarketType(CnCloudMarketType cnCloudMarketType) {
        return save(cnCloudMarketType);
    }

    /**
     * 修改云市场类型
     *
     * @param cnCloudMarketType 云市场类型
     * @return 结果
     */
    @Override
    public boolean updateCnCloudMarketType(CnCloudMarketType cnCloudMarketType) {
        return updateById(cnCloudMarketType);
    }

    /**
     * 批量删除云市场类型
     *
     * @param ids 需要删除的云市场类型主键
     * @return 结果
     */
    @Override
    public boolean deleteCnCloudMarketTypeByIds(List<String> ids) {
        long count = cnCloudMarketService.count(new LambdaQueryWrapper<CnCloudMarket>()
                .in(CnCloudMarket::getCloudMarketGeneralClassification, ids)
                .or().in(CnCloudMarket::getCloudMarketIndustryClassification, ids));
        if(count > 0){
            throw new ServiceException("类型已使用，不可删除");
        }
        return removeBatchByIds(ids);
    }

    /**
     * 删除云市场类型信息
     *
     * @param id 云市场类型主键
     * @return 结果
     */
    @Transactional
    @Override
    public boolean deleteCnCloudMarketTypeById(String id) {
      return removeById(id);
    }

    @Override
    public Map typeList() {

        List<CnCloudMarketType> list = this.list();
        Map<String, Object> map = new HashMap<>();
        map.put("cloudMarketGeneralClassification", list.stream().filter(v -> v.getType().equals("1")).collect(Collectors.toList()));
        map.put("cloudMarketIndustryClassification", list.stream().filter(v -> v.getType().equals("2")).collect(Collectors.toList()));

        return map;
    }

}
